<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Products extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'products_model', 'products');
		$this->load->helper('text');
		$this->load->module_language('member', 'product');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
	}
	
 
	public function index() 
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'List my Products in Store';
		$data['description'] = 'List my Products in Store';
		$this->access->restrict();
		$this->load->library('basic_library');
		
		
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = base_url().'member/products/';
		$config['total_rows'] = $this->products->count_all_my_product($this->user->id);
		//echo $config['total_rows'];
		$config['per_page'] = 30;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		$data['myproducts'] = $this->products->get_my_products($this->user->id,$config['per_page'], $page);
		$data['links'] = $this->pagination->create_links();
		//$data['products'] = $this->store->get_product_of_category($id,$config['per_page'], $page);
		
		$this->_template['page']	= 'users/products/show';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function add()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Add new Product';
		$data['description'] = 'Add new Product';
		//use for multi upload icon, thumbs
		//$this->load->library('form_validation');
		$this->load->library('upload');
		//$this->load->library('validation');
		$this->load->helper('form');
		$config['upload_path']   = './uploads/products/images'; //if the files does not exist it'll be created
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']   = '4000'; //size in kilobytes
		$config['encrypt_name']  = TRUE;
		$this->upload->initialize($config);
		$uploaded = $this->upload->up(); //Pass true if you want to create the index.php files
		
		//var_dump($uploaded['success']);
		//var_dump($uploaded);
		//var_dump($uploaded);
		//foreach($uploaded['success'] as $file){
		//	echo $file['file_name'];
		//}
		
		//$file1['file_name']
		//endforeach;
		
		$data['categories'] = $this->products->get_all_category();
		$data['apps'] = $this->products->get_all_app();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('productname', 'lang:form_product_name', 'required|max_length[50]|xss_clean|callback_check_productname');
		$this->form_validation->set_rules('alias', 'lang:form_alias', 'callback_check_alias');
		$this->form_validation->set_rules('price', 'lang:form_price', 'required|xss_clean|trim|numeric');
		$this->form_validation->set_rules('app','lang:form_application','required|numeric');
		$this->form_validation->set_rules('category','lang:form_category','required|numeric');
		$this->form_validation->set_rules('description','lang:form_descrtiption','required|max_length[255]');
		/*if($_FILES['file_1']=='' || empty($_FILES['file_2']))
		{
			$this->form_validation->set_rules('file_1','lang:form_thumbs1','required');
		}*/

		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->products->add_product($userid, $uploaded['success']);
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('member/products/', 'refresh');
		}	
		$this->_template['page']	= 'users/products/add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function check_alias()
	{
        if($this->products->check_alias())
        {
            $this->form_validation->set_message('check_alias','Alias already exist please choose onther one');
            return false;
        }
        else
        {
            
            return true;
        }
    }
	
	public function check_productname()
	{
        if($this->products->check_productname())
        {
            $this->form_validation->set_message('check_productname','Product name already exist please choose onther one');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
	public function file_required()
	{ //var_dump($_FILES['file_1']['name']);
		if(empty($_FILES['file_1']['name']))
		{	
			$this->form_validation->set_message('file_required', 'The %s field required');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	public function delete($id)
	{
		$this->products->delete_product($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));
		redirect('member/products', 'refresh');
	}

	//Edit product
	public function edit($id)
	{
		//metadata
		$data['ItemID']		 = 'User';
		$data['title']		 = "Edit Product"; 
		$data['description'] = "Edit Product"; 
		$data['categories'] = $this->products->get_all_category();
		$data['apps'] 		= $this->products->get_all_app();
		$data['product'] 	= $this->products->get_product_info($id);
		
		$this->form_validation->set_rules('productname', 'lang:form_product_name', 'required|max_length[50]|xss_clean');
		if($data['product']->alias === strtolower(auto_alias($this->input->post('alias'))) || $this->input->post('alias')=='')
		{
			$this->form_validation->set_rules('alias', 'lang:form_alias', 'max_length[250]');
		}
		else
		{
			$this->form_validation->set_rules('alias', 'lang:form_alias', 'callback_check_alias');
		}
		
		$this->form_validation->set_rules('price', 'lang:form_price', 'required|xss_clean|trim|numeric');
		$this->form_validation->set_rules('app','lang:form_application','required|numeric');
		$this->form_validation->set_rules('category','lang:form_category','required|numeric');
		$this->form_validation->set_rules('description','lang:form_descrtiption','required|max_length[255]');					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$config['upload_path'] = './uploads/products/images';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '800';
		$config['max_height']  = '600';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		if($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name']=='')
			{
				$this->products->edit_product($id);
				$this->session->set_flashdata('message', lang('successfully_edited'));	
				redirect('member/products/', 'refresh');
			}
			else
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('message', $this->upload->display_errors());
					redirect($this->uri->uri_string());
				}
				else
				{
					$this->products->edit_product($id);
					$this->session->set_flashdata('message', lang('successfully_edited'));	
					redirect('member/products/', 'refresh');
				}		
			}	
		}	
		$this->_template['page']	= 'users/products/edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->products->change_status_product($id,1);
		redirect('member/products', 'refresh');
		
	}
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->products->change_status_product($id,0);
		redirect('member/products', 'refresh');
	}
	
	public function filemanager($id)
	{		
		$data['ItemID'] = 'User';
		$data['title']	= "Upload Game File";
		$data['description'] = "Upload Game File";
		$this->load->library('validation');
		$this->load->helper('form');
		
		//config to upload file game
		$config['upload_path'] = './uploads/products/files/';
		$config['allowed_types'] = 'ipk|jar|apk|sis';
		$config['max_size'] = '10000';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		$this->form_validation->set_rules('link','lang:form_link','max_length[250]');
		$this->form_validation->set_error_delimiters('', '<br />');
		$data['product'] = $this->products->get_product_info($id);
		
		if($this->form_validation->run() == TRUE)
		{
			$up =  $this->input->post("uploadfile");
			if($up==="uploadfile")
			{	
				//if userfile upload = null
				if($_FILES['userfile']['name'] == '')
				{
					$this->session->set_flashdata('message', lang('successfully_addfile'));	
					//$this->products->updatefile($id);
					redirect('member/products', 'refresh');
				}
				else
				{
					//do upload and insert file to database
					$this->upload->do_upload();
					//var_dump($this->upload->do_upload());
					$this->products->updatefile($id);
					$this->session->set_flashdata('message', lang('successfully_addfile'));	
					redirect('member/products', 'refresh');
				}
			}
			else
			{
				//insert link to database
				//var_dump($this->input->post("link")); exit();
				$this->products->updatefile2($id);
				$this->session->set_flashdata('message', lang('successfully_addfile'));	
				redirect('member/products', 'refresh');
			}
		}
		else 
		{	
			$this->_template['page']	= 'users/products/filemanager';
			$this->system_library->load($this->_template['page'], $data);
		}
		
		
	}
	public function gallery($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('productid');
		}
		
		// metadata
		$data['ItemID']		 = 'User';
		$data['title']		 = "Edit Gallery Product"; 
		$data['description'] = "Edit Gallery Product";
		
		$config['upload_path'] = './uploads/products/images/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '800';
		$config['max_height']  = '600';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		$data['product'] = $this->products->get_product_info($id);
		$data['gallery'] = $this->products->get_gallery_product($id);
		
		$this->form_validation->set_rules('userfile','lang:form_link','');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name'] == null)
			{
				$this->session->set_flashdata('message', lang('no_file_upload'));
				redirect('member/products/gallery/'.$id);
			}
			else
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('message', $this->upload->display_errors());
					redirect('member/products/gallery/'.$id);
				}
				else
				{
					$this->products->upload_product_thumb($id);
					$this->session->set_flashdata('message', lang('successfully_created'));
					redirect('member/products/gallery/'.$id);
				}
			}
			
		}
		$this->_template['page']	= 'users/products/gallery';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function delete_item()
	{
		$id = $this->input->post('id');
		$json = array();
		if($this->products->delete_thumb_product($id))
		{
			$json['result'] = 1;
		}
		else
		{
			$json['result'] = 0;
		}
		
		echo json_encode($json);
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/products.php */